package com.af.monitorModule.controller.task;

import com.af.monitorModule.generator.entity.TimedTask;
import com.af.monitorModule.generator.service.TimedTaskService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TimedTaskService timedTaskService;

    /**
     * 根据监控ID获取对应公司的定时任务
     * @param monitor
     * @return
     */
    @GetMapping("/get/{monitor}")
    public JSONObject getRunTask(@PathVariable String monitor){
        return timedTaskService.getRunTask(monitor);
    }

    /**
     * 调用完成后更新调用时间
     * @param task
     * @return
     */
    @PostMapping("/over/{task}")
    public String sendOver(@PathVariable String task){
        return timedTaskService.sendOver(task);
    }


    /**
     * 获取任务列表
     * @param page
     * @param limit
     * @param search
     * @return
     */
    @GetMapping("/findTask")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public JSONObject findTask(String page, String limit, String search){
        JSONObject searchData = JSONObject.parseObject(search);
        return timedTaskService.findTask(page,limit,searchData);
    }

    @PostMapping("/changeState/{id}")
    public JSONObject changeState(@PathVariable String id){
        return timedTaskService.changeState(id);
    }


        @PostMapping("/batchChange")
    public JSONObject batchStop(@RequestBody JSONObject ids){
        return timedTaskService.batchStop(ids);
    }


    @PostMapping("/addTask")
    public JSONObject addTask(@RequestBody TimedTask timedTask){
        return timedTaskService.addTask(timedTask);
    }

    @PostMapping("/update/{id}")
    public JSONObject update(@RequestBody TimedTask timedTask,@PathVariable String id){
        return timedTaskService.updateTask(timedTask,id);
    }
    //立即执行任务接口
    @PostMapping("/runNow/{id}")
    public JSONObject runNow(@PathVariable String id){
        return timedTaskService.runNow(id);
    }
}
